package com.myclasses;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

import com.google.gdata.data.DateTime;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.When;

public class EventBean{
	private String comment;
	private String startTime;
	private String endTime;
	private CalendarEventEntry calendarEntry;
	private boolean isChanged = false;
	private List<String> errorList = new ArrayList();
	private static SimpleDateFormat dateFormat;
	
	static{
		dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Warsaw"));
	}

	
	public EventBean(){
		this.calendarEntry = new CalendarEventEntry();
		When time = new When();
		Calendar calendar = new GregorianCalendar();
	    DateTime start = new DateTime(calendar.getTime(), TimeZone.getTimeZone("Europe/Warsaw"));
	    calendar.add(Calendar.MINUTE, 30);
	    DateTime end = new DateTime(calendar.getTime(), TimeZone.getTimeZone("Europe/Warsaw"));
		time.setStartTime(start);
		time.setEndTime(end);
		this.calendarEntry.getTimes().clear();
		this.calendarEntry.addTime(time);
	}
	
	public EventBean(CalendarEventEntry calendarEntry){
		this.calendarEntry = calendarEntry;
	}
	
	
	
	public String getErrors(){
		StringBuffer sb = new StringBuffer();
		for (String errorMsg : this.errorList) {
			sb.append(errorMsg+"\n");
		}
		return sb.toString();
	}
	
	
	
	public String addToCalendar(){
		verify();
		if(this.errorList.size()==0){
			try{
				CalendarControler.getInstance().addNewEvent(this.calendarEntry);
			}catch (Exception e) {
				this.errorList.add("Blad przy zapisywaniu zdarzenia w google calendar: " + e.getMessage());
				return "addEvent_failure";
			}
			return "addEvent_success";
		}else{
			return "addEvent_failure";
		}
	}
	
	private void verify(){		
		this.errorList.clear();
		if(getComment().length()==0){
			errorList.add("Brak nazwy zdarzenia");
		}
		
		//dates
		Date eventStart=null;
		try{
			eventStart = dateFormat.parse(this.startTime);
		}catch (Exception e) {
			errorList.add("Niepoprawny format daty dla poczatku zdarzenia");
		}
		Date eventEnd=null;
		try{
			eventEnd = dateFormat.parse(this.endTime);
		}catch (Exception e) {
			errorList.add("Niepoprawny format daty dla konca zdarzenia");
		}

		When eventTime = new When();
		if(eventStart!=null && eventEnd!=null){
			eventTime.setStartTime(new DateTime(eventStart, TimeZone.getTimeZone("Europe/Warsaw")));
			eventTime.setEndTime(new DateTime(eventEnd, TimeZone.getTimeZone("Europe/Warsaw")));
			this.calendarEntry.getTimes().clear();
			this.calendarEntry.getTimes().add(eventTime);
		}
				
	}
	

	public String getComment(){
		if(this.calendarEntry.getTitle()!=null){
			return this.calendarEntry.getTitle().getPlainText();
		}else{
			return "";
		}
	}
	public void setComment(String comment){
		this.isChanged = true;
		this.calendarEntry.setTitle(new PlainTextConstruct(comment));
	}

	public String getStartTime(){
		if(this.calendarEntry.getTimes().size()>0)
			return this.calendarEntry.getTimes().get(0).getStartTime().toUiString();
		else
			return "";
	}
	public void setStartTime(String startTime){
		this.startTime = startTime;
	}
	
	public String getEndTime(){
		if(this.calendarEntry.getTimes().size()>0)
			return this.calendarEntry.getTimes().get(0).getEndTime().toUiString();
		else
			return "";
	}
	public void setEndTime(String endTime){
		this.endTime = endTime;
	}	
}